package com.runshu.newsmartark.dbutil;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.TypeConverters;

import com.runshu.newsmartark.entity.UserInfoEntity;

//注解指定了database的表映射实体数据以及版本等信息
@Database(entities = {MenTmpTableEntity.class,ProAllEntity.class, UserInfoEntity.class, FaceEntity.class, ProSelfEntity.class,OperRecordfEntity.class, LogListEntity.class}, version = 2)
@TypeConverters({Converters.class})
public abstract class SmartRoomDatabase extends androidx.room.RoomDatabase {
    //RoomDatabase提供直接访问底层数据库实现，我们通过定义抽象方法返回具体Dao
    //然后进行数据库增删该查的实现。
    public abstract FaceDao faceDao();

    public abstract UserDao userDao();

    public abstract ProAllDao proAllDao();

    private static volatile SmartRoomDatabase instance;

    public static SmartRoomDatabase getInstance(Context context) {
        if (instance == null) {
            synchronized (SmartRoomDatabase.class) {
                if (instance == null) {
                    instance = Room.databaseBuilder( context.getApplicationContext(), SmartRoomDatabase.class, "RoomSmartDB.db" )
                            .allowMainThreadQueries()
                            .fallbackToDestructiveMigrationFrom(  )
//                            .addMigrations(MIGRATION_1_2)
                            .build();
                }
            }
        }
        return instance;
    }

//    static final Migration MIGRATION_1_2 = new Migration( 1, 2 ) {
//        @Override
//        public void migrate(@NonNull SupportSQLiteDatabase database) {
//            database.execSQL("ALTER TABLE OperRecordfEntity "
//                    + " ADD COLUMN userName TEXT");
//
//        }
//    };

    //    static final Migration MIGRATION_2_3 = new Migration(2, 3) {
    //        @Override
    //        public void migrate(@NonNull SupportSQLiteDatabase database) {
    //
    //        }
    //    };
    public void release() {
        if (instance != null) {
            instance = null;
        }
    }
}